
function adjustBody(scrollBody) {
	var wrapper = $(scrollBody).closest('.dataTables_wrapper');
	var w_pos = $(wrapper).position();
	$(scrollBody).height($('#main').height() - w_pos.top - (wrapper.height() - $(scrollBody).height()))
		.width($(scrollBody).width() - 2);
}
	
$(document).ready(function() {
	var tables = $('div.index table, table.index').each(function() {
		var t_pos = $(this).position();
		var defaults = $.extend({ 
			"bJQueryUI": true,
			"bScrollInfinite": true,
			"sScrollY": ($('#main').height() - t_pos.top - 100) + "px",
			"bPaginate": false
			
		});
		
		var settings = $(this).data('settings');
		var oTable = $(this).dataTable($.extend(defaults, settings));
		
		$(window).resize(function() {
			oTable.fnAdjustColumnSizing();
		});
		
		// adjust body after converting to dataTable
		adjustBody($(this).closest('.dataTables_scrollBody'));
	});
	
	// event on window resize
	$(window).resize(function() {
		$('.dataTables_scrollBody').each(function() { adjustBody(this); });
	});

});

jQuery.fn.dataTableExt.oSort['lil_date-asc']  = function(a,b) {
	var arDatea = a.split('<?php echo Configure::read('dateSeparator'); ?>');
	var arDateb = b.split('<?php echo Configure::read('dateSeparator'); ?>');
	var sOrder = '<?php echo Configure::read('dateFormat'); ?>';
	
	var x = (arDatea[sOrder.indexOf('Y')] + arDatea[sOrder.indexOf('M')] + arDatea[sOrder.indexOf('D')]) * 1;
	var y = (arDateb[sOrder.indexOf('Y')] + arDateb[sOrder.indexOf('M')] + arDateb[sOrder.indexOf('D')]) * 1;
	
	return ((x < y) ? -1 : ((x > y) ?  1 : 0));
};
jQuery.fn.dataTableExt.oSort['lil_date-desc']  = function(a,b) {
	var arDatea = a.split('<?php echo Configure::read('dateSeparator'); ?>');
	var arDateb = b.split('<?php echo Configure::read('dateSeparator'); ?>');
	var sOrder = '<?php echo Configure::read('dateFormat'); ?>';
	
	var x = (arDatea[sOrder.indexOf('Y')] + arDatea[sOrder.indexOf('M')] + arDatea[sOrder.indexOf('D')]) * 1;
	var y = (arDateb[sOrder.indexOf('Y')] + arDateb[sOrder.indexOf('M')] + arDateb[sOrder.indexOf('D')]) * 1;
	
	return ((x < y) ? 1 : ((x > y) ?  -1 : 0));
};

jQuery.fn.dataTableExt.oSort['lil_float-asc']  = function(a,b) {
	var x = LilFloatStringToFloat(a);
	var y = LilFloatStringToFloat(b);
	return ((x < y) ? -1 : ((x > y) ?  1 : 0));
};
jQuery.fn.dataTableExt.oSort['lil_float-desc']  = function(a,b) {
	var x = LilFloatStringToFloat(a);
	var y = LilFloatStringToFloat(b);
	return ((x < y) ? 1 : ((x > y) ?  -1 : 0));
};